//
//  XHHomeViewModel.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-29.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHHomeViewModel.h"
#import "XHURLString.h"
#import "AFNetworking.h"


@interface XHHomeViewModel()
@property (nonatomic ,strong) XHURLString *urlString;
@end
@implementation XHHomeViewModel


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.urlString = [[XHURLString alloc] init];
        
    }
    return self;
}
- (void)getAllCategory
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    NSDictionary *params = @{@"areaCode": self.urlString.areaCode};
    
    [manager POST:[self.urlString getAllCategory] parameters:params success:^(AFHTTPRequestOperation*operation, id responseObject) {
        [self writeData:responseObject];
        
       
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        
        
    }];

}

- (void)writeData:(id)data
{
    
    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath = [paths objectAtIndex:0];
    
    
    NSString *filePath =[plistPath stringByAppendingPathComponent:@"AllCagtegory.json"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        
        NSError *error;
        [fileManager removeItemAtPath:filePath error:&error];
        
    }
    
    NSMutableData *content= [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:content];
    [archiver encodeObject:data forKey:@"shouyang"];
    [archiver finishEncoding];
    [fileManager createFileAtPath:filePath contents:content attributes:nil];
    
}


@end
